package com.sunzn.media.library.adpt;

import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.sunzn.media.library.R;
import com.sunzn.media.library.bean.Album;

public class AlbumAdapter extends RecyclerViewCursorAdapter<AlbumAdapter.ViewHolder> {

    public class ViewHolder extends RecyclerView.ViewHolder {

        private final AppCompatImageView cover;
        private final AppCompatTextView title;
        private final AppCompatTextView count;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        int position = getAdapterPosition();
                        Cursor cursor = getCursor();
                        cursor.moveToPosition(position);
                        Album album = Album.valueOf(cursor);
                        listener.onAlbumClick(album);
                    }
                }
            });
            cover = itemView.findViewById(R.id.media_album_cover);
            title = itemView.findViewById(R.id.media_album_title);
            count = itemView.findViewById(R.id.media_album_count);
        }

        public AppCompatImageView getCover() {
            return cover;
        }

        public AppCompatTextView getTitle() {
            return title;
        }

        public AppCompatTextView getCount() {
            return count;
        }
    }

    public interface OnAlbumClickListener {

        void onAlbumClick(Album album);

    }

    private OnAlbumClickListener listener;

    public void setOnAlbumClickListener(OnAlbumClickListener listener) {
        this.listener = listener;
    }

    public AlbumAdapter(OnAlbumClickListener listener) {
        super(null);
        setOnAlbumClickListener(listener);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.media_album_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(ViewHolder holder, Cursor cursor) {
        Album album = Album.valueOf(cursor);
        holder.getTitle().setText(album.getDisplayName());
        holder.getCount().setText(String.valueOf(album.getCount()));
        Glide.with(holder.getCover()).asBitmap().load(album.getCoverUri()).apply(new RequestOptions().centerCrop()).into(holder.getCover());
    }

    @Override
    protected int getItemViewType(int position, Cursor cursor) {
        return 0;
    }

}
